<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('菜单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="menu-form">
                <div class="select-list">
                    <ul>
                        <li>
                            配置名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.treeTable.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="add()">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary" onclick="edit()">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-primary" onclick="publish()">
                <i class="fa fa-edit"></i> 发布
            </a>
            <a class="btn btn-danger" onclick="remove()">
                <i class="fa fa-edit"></i> 删除
            </a>
            <a class="btn btn-danger" onclick="removeRec()">
                <i class="fa fa-edit"></i> 递归删除
            </a>
            <a class="btn btn-warning" onclick="exportConfig()">
                <i class="fa fa-download"></i> 导出
            </a>
            <a class="btn btn-warning" onclick="exportConfigProperties()">
                <i class="fa fa-download"></i> 导出SpringBootProperties
            </a>
            <a class="btn btn-info" id="expandAllBtn">
                <i class="fa fa-exchange"></i> 展开/折叠
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-tree-table" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    String.prototype.endWith=function(str){
        var reg=new RegExp(str+"$");
        return reg.test(this);
    }

    $(function () {
        var options = {
            code: "menuId",
            parentCode: "parentId",
            uniqueId: "menuId",
            expandAll: false,
            expandFirst: true,
            url: ctx + "config/center/env?url=" + [[${url}]],
            createUrl: ctx + "config/center/node/add?url={id}",
            updateUrl: ctx + "config/center/node/modify?url={id}",
            modalName: "菜单",
            columns: [{
                field: 'selectItem',
                radio: true
            },
                {
                    title: '配置名称',
                    field: 'name',
                    formatter: function (value, row, index) {
                        return '<a onclick="openDetail(\'' + row.name + '\', \'' + row.url + '\')">' + row.name + '</a>'
                    }
                },
                {
                    field: 'url',
                    title: 'url',
                    visible: false
                },
                {
                    field: 'text',
                    title: '配置值',
                    align: "left",
                },
                {
                    field: 'remark',
                    title: '备注',
                    align: "left"
                },
                {
                    title: '操作',
                    width: '15%',
                    align: "left",
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.url.indexOf("online-machines") >= 0) {
                            if(!row.url.endWith("online-machines")) {
                                actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="detail(\'' + row.url + '\')"><i class="fa fa-eye"></i>查看</a>   ');
                            }
                        } else {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.add(\'' + row.url + '\')"><i class="fa fa-plus"></i>新增</a>   ');
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.url + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.treeTable.init(options);
    });

    function remove() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        $.modal.confirm("确定删除吗？", function () {
            var url = ctx + "config/center/node/del?url=" + urls[0];
            $.operate.get(url);
        });
    }

    function removeRec() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        $.modal.confirm("确定递归全部删除吗？", function () {
            var url = ctx + "config/center/node/delRec?url=" + urls[0];
            $.operate.get(url);
        });
    }

    function detail(url) {
        $.modal.open("配置详情", ctx + 'config/center/node/detail?url=' + url);
    }
    
    function add() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        $.modal.open("添加配置", ctx + 'config/center/node/add?url=' + urls[0]);
    }

    function edit() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        $.modal.open("修改配置", ctx + 'config/center/node/modify?url=' + urls[0]);
    }

    function publish() {
        $.modal.confirm("确定发布修改参数？", function () {
            var url = ctx + "config/center/publish";
            $.operate.post(url, {"path": [[${url}]]});
        });
    }

    function exportConfig() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        window.location.href = ctx + "config/center/config.properties?path=" + urls[0];
    }

    function exportConfigProperties() {
        var urls = $.treeTable.selectColumns("url");
        if (urls.length == 0 || urls.length > 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        window.location.href = ctx + "config/center/application.properties?path=" + urls[0];
    }

    function openDetail(name, url) {
        name = [[${env}]] + '-' + name;
        parent.menuItem(ctx + 'config/center/detail?url=' + url, name, name);
    }

</script>
</body>
</html>